package com.sky.handler;

import com.baomidou.mybatisplus.extension.api.R;
import com.sky.exception.BaseException;
import com.sky.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器，处理项目中抛出的业务异常
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 捕获业务异常
     * @param ex
     * @return
     */
    @ExceptionHandler
    public Result<String> exceptionHandler(BaseException ex){
        log.error("异常信息：{}", ex.getMessage());
        return Result.error(ex.getMessage());
    }


    /**
     * 全局默认异常处理器
     * @param ex
     * @return
     */
    @ExceptionHandler
    public Result<String> defultExceptionHandle(Exception ex){
        log.error("异常信息：{}",ex.getMessage());
        ex.printStackTrace();//输出控制台
        return Result.error(ex.getMessage());
    }


    @ExceptionHandler
    public Result<String> exceptionDuplicationKey(DuplicateKeyException ex){
        log.error("异常信息：{}",ex.getMessage());
        ex.printStackTrace();//输出控制台
        return Result.error("用户名已存在");
    }



}
